package com.chuangke.common.express.function.math;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorDecimal;
import com.googlecode.aviator.runtime.type.AviatorObject;

public class MathSumFunction extends AbstractFunction {

	
    private static final long serialVersionUID = -5223560162109718127L;

	@SuppressWarnings("unchecked")
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
		List<BigDecimal> datas = (List<BigDecimal>) arg1.getValue(env);
		if (datas == null || datas.isEmpty()) {
			return new AviatorDecimal(0);
		}
		
		BigDecimal sum = new BigDecimal("0.0");
		for (BigDecimal data : datas) {
			if (data != null) {
				sum = sum.add(data);
			}
		}
		return new AviatorDecimal(sum);
	}

	@Override
	public String getName() {
		return "sum";
	}

}
